package com.eims.sls_app.ui;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import bls.juejin.cn.R;
import com.eims.sls_app.indicator.widget.TabPageIndicator;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.view.MyAccountsInfoDialog;
import com.eims.sls_app.view.MyFundsInfoDialog;
import com.eims.sls_app.wheel.widget.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 功能：我的全部cp类，显示全部cp 版本
 * 
 * @author Administrator
 */
public class MyAllAccountLotteryActivity extends MyFragmentActivity implements
		OnClickListener {

	private ViewPager mViewPager;
	private ImageButton btn_back;
	private TabPageIndicator my_all_lottery_indicator;
	private List<View> listViews;
	private View view_all, view_follow, view_win, view_waitWin, view_join;

	private LayoutInflater mInflater;

	private AllAccountBetLottery allBet = null;
	private FollowAccountBetLottery followBet = null;
	private WinAccountBetLottery winBet = null;
	private WaitAccountWinBetLottery waitBet = null;
	private JoinAccountBetLottery joinBet = null;

	private ImageButton imageDateButton;
	private int currentPage = 0;

	/*private static final String[] TITLES = new String[] { "全部", "投注", "中奖",
			"充值", "提款" };*/
	private static final String[] TITLES = new String[] { "全部", "投注", "中奖",
		"充值"};

	private TextView tvAccount;

	private MyAccountsInfoDialog infoDialog;

	private ArrayWheelAdapter<String> yearAdapter;
	private ArrayWheelAdapter<String> monthAdapter;

	private String[] yearItems = { "2009", "2010", "2011", "2012", "2013",
			"2014", "2015", "2016","2017","2018" };

	private String[] monthItems = { "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "10", "11", "12" };

	private int year;
	private int month;
	private int day;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_all_lottery);
		App.activityS.add(this);
		init();
	}
	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		Resources res = super.getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config, res.getDisplayMetrics());
		return res;
	}

	/** 初始化 根据传入的类型进行加载数据 */
	private void init() {

		currentPage = getIntent().getIntExtra("index", 0);
		mViewPager = (ViewPager) this.findViewById(R.id.vp_myViewPager);
		btn_back = (ImageButton) findViewById(R.id.btn_back);
		imageDateButton = (ImageButton) findViewById(R.id.funds_date_btn);
		imageDateButton.setVisibility(View.VISIBLE);
		imageDateButton.setOnClickListener(this);

		initViewPager();

		mViewPager.setAdapter(new MyPagerAdapter());
		my_all_lottery_indicator = (TabPageIndicator) findViewById(R.id.my_all_lottery_indicator);
		my_all_lottery_indicator.setViewPager(mViewPager, currentPage);

		Calendar now = Calendar.getInstance();
		year = now.get(Calendar.YEAR);
		month = now.get(Calendar.MONTH) + 1;
		day = AppTools.getLastDay(year, month);
		yearAdapter = new ArrayWheelAdapter<String>(yearItems);
		monthAdapter = new ArrayWheelAdapter<String>(monthItems);
		infoDialog = new MyAccountsInfoDialog(this, R.style.dialog,
				yearAdapter, monthAdapter, year, month);
		tvAccount = (TextView) findViewById(R.id.tv_account);
		tvAccount.setText("账户明细");
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initViewPager() {
		listViews = new ArrayList<View>();
		mInflater = getLayoutInflater();

		view_all = mInflater.inflate(R.layout.center_all_account_lottery, null);
		view_all.setTag("all");

		view_follow = mInflater.inflate(R.layout.center_all_account_lottery,
				null);
		view_follow.setTag("follow");

		view_win = mInflater.inflate(R.layout.center_all_account_lottery, null);
		view_win.setTag("win");

		view_waitWin = mInflater.inflate(R.layout.center_all_account_lottery,
				null);
		view_waitWin.setTag("wait");

		/*view_join = mInflater
				.inflate(R.layout.center_all_account_lottery, null);
		view_join.setTag("join");*/

		listViews.add(view_all);
		listViews.add(view_win);
		listViews.add(view_waitWin);
		listViews.add(view_follow);
		//listViews.add(view_join);
	}

	private class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return listViews.size();
		}

		/**
		 * 从指定的position创建page
		 * 
		 * @param collection
		 *            ViewPager容器
		 * @param position
		 *            The page position to be instantiated.
		 * @return 返回指定position的page，这里不需要是一个view，也可以是其他的视图容器.
		 */
		@Override
		public Object instantiateItem(View collection, int position) {
			((ViewPager) collection).addView(listViews.get(position), 0);

			View v = listViews.get(position);
			String tag = (String) v.getTag();
			if ("all".equals(tag)) {
				if (null == allBet) {//全部

					allBet = new AllAccountBetLottery(getApplicationContext(),
							v, year, month, day);
					allBet.init();

				}
			}
			if ("win".equals(tag)) {
				if (null == winBet) {//投注
					winBet = new WinAccountBetLottery(getApplicationContext(),
							v, year, month, day);
					winBet.init();
				}
			}
			if ("wait".equals(tag)) {
				if (null == waitBet) {//中奖
					waitBet = new WaitAccountWinBetLottery(
							getApplicationContext(), v, year, month, day);
					waitBet.init();
				}
			}
			if ("follow".equals(tag)) {
				if (null == followBet) {//充值
					followBet = new FollowAccountBetLottery(
							getApplicationContext(), v, year, month, day);
					followBet.init();
				}
			}
			

			/*if ("join".equals(tag)) {
				if (null == joinBet) {//提款
					joinBet = new JoinAccountBetLottery(
							getApplicationContext(), v, year, month, day);
					joinBet.init();
				}
			}*/

			return v;
		}

		/**
		 * 55. * <span style='font-family:
		 * "Droid Sans";'>从指定的position销毁page</span> 56. * 57. * 58. *<span
		 * style='font-family: "Droid Sans";'>参数同上</span> 59.
		 */

		@Override
		public void destroyItem(View collection, int position, Object view) {
			((ViewPager) collection).removeView(listViews.get(position));

		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == (object);
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position % TITLES.length];
		}
	}

	/** 异步任务 */
	class MyAsynTask extends AsyncTask<Void, Integer, String> {
		/** 在后台执行的程序 */
		@Override
		protected String doInBackground(Void... params) {
			return "0";
		}

		@Override
		protected void onPostExecute(String result) {
			if (null == result)
				return;
			super.onPostExecute(result);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.funds_date_btn:
			infoDialog.show();
			break;

		default:
			break;
		}
	}

	public void refresh(int year, int month, int day) {
		// if (list_fragments != null && !list_fragments.isEmpty()) {
		// for (Fragment iterable_element : list_fragments) {
		// // ((FunsdsFragment) iterable_element).Refresh(year, month, day);
		// }
		// }
		if (listViews.size() > 0) {

			for (View v : listViews) {
				String tag = (String) v.getTag();
				if (tag.equals("all")) {
					if (null != allBet) {

						allBet.Refresh(year, month, day);
					}

				} else if (tag.equals("follow")) {

					if (null != followBet) {

						followBet.Refresh(year, month, day);
					}

				} else if (tag.equals("win")) {

					if (null != winBet) {
						winBet.Refresh(year, month, day);
					}

				} else if (tag.equals("wait")) {

					if (null != waitBet) {
						waitBet.Refresh(year, month, day);
					}

				} /*else if (tag.equals("join")) {

					if (null != joinBet) {

						joinBet.Refresh(year, month, day);
					}

				}*/

			}

		}
	}
}
